import org.junit.Test;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.io.File;
import java.io.FileInputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;




public class errorhandling {
	//public int xRow; int xCol;
	public int iWait;
	
	@Test
	public void qS1() throws Exception{
		
		
		String[][] xTC, xTS;
		String vKW, vXpath, vTestData;
		FirefoxDriver myD = new FirefoxDriver();
	
		
		String xPath = "C:/Priya/Silverback 4.3/Automation/QService.xls";
		
		//Read the entire TC and TS
		xTC = XL1.xlRead(xPath,"TestCases");
		xTS = XL1.xlRead(xPath, "TestSteps");
		
		
		System.out.println("Test Step sheet is" + xTS);
		
		//Go to each row in TC and identify which TC to execute
		
		for (int i = 1; i<xTC.length; i++) {   //Go to each row in TC
			System.out.println("TCID  is " + xTC[i][0]);
			
			if (xTC[i][3].equalsIgnoreCase("Y")) {
				
				System.out.println("Yes, execute this");
				
			
			//go to every test step if it is y
			for (int j=1; j<xTS.length; j++){
				//Thread.sleep(iWait);
				System.out.println("TSID is" +xTS[i][0]);
				Thread.sleep(iWait);
				if (xTC[i][0].equalsIgnoreCase(xTS[j][1])){
					//Thread.sleep(iWait);
					System.out.println("KW to execute is " + xTS[j][4]);
					Thread.sleep(iWait);
					System.out.println("xpath to execute is " + xTS[j][5]);
					System.out.println("Data to execute is " + xTS[j][6]);
					
					vKW = xTS[j][4] ;
					vXpath = xTS[j][5];
					vTestData = xTS[j][6];
					String vError = "Pass";
					// call the corresponding KW function
					
					try{
						
						if (vKW.equalsIgnoreCase("enterText")) {
							KW.enterText(myD, vXpath, vTestData);
						}
							if (vKW.equalsIgnoreCase("navigateBrowser")){
							KW.navigateBrowser(myD, vTestData);
							}
								if(vKW.equalsIgnoreCase("clickElement")){
								KW.clickElement(myD, vXpath);
								}
									if (vKW.equalsIgnoreCase("verifyText")){
										vError = KW.verifyText(myD, vXpath, vTestData);
										System.out.println("Test is a" + vError );
									}
									
									if (vKW.equalsIgnoreCase("readText")){
										vError = KW.readText(myD, vXpath);
										System.out.println("Test is a" + vError); // These methods return something and they are only print. If this line fails, it still going as a pass.They will not throw exceptions
										// 
									}
									if (vKW.equalsIgnoreCase("waitTime")){
										KW.waitTime(3000);
									}
									if (vError.equalsIgnoreCase("Pass")){
									
									System.out.println("Pass" + xTC[i][0] + "_" + vKW);
									xTS[j][8] = "Pass";
						
									} else {
										System.out.println("Fail" + xTC[i][0] + "_" + vKW);
										xTS[j][8] = "Fail";
										xTS[j][9] = "Error happened as function returned Fail" ;
										
									}
					} catch (Exception myError){ //If an error occurred then the code will go to the catch exception. Anything to do with Application will get captured here.
						System.out.println("Fail" + xTC[i][0] + "_" + vKW);   
						xTS[j][8] = "Fail";
						//System.out.println("Error happened" + myError);
						xTS[j][9] = "Error happened" + myError;
						
					}
					
								
				}
			}
			}
		}
		
		//Write back the test steps to Excel
		
		
		XL1.writeXL("C:\\Priya\\Silverback 4.3\\KDFResults.xls", "TestSteps Results", xTS);
	}
}
		
		
						
						

		
	

			
		
		